package com.controller;

import com.handler.BizException;
import excel.CarExcelReq;
import excel.file.FileUtil;
import excel.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import 爬虫.DongCheDiCarDto;
import 爬虫.PaChongUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author CHL
 * @ClassName: PachongController
 * @Description:
 * @date 2023/4/23 15:39
 * @Version 1.0
 */
@RestController
@RequestMapping("/pachong")
@Api(tags = "爬虫类")
public class PachongController {


    @PostMapping("/exportDongCheDi")
    @ApiOperation(value = "导出懂车帝")
    public void exportMaintain(HttpServletResponse response) {

        // 懂车帝数据导出
        String[] cityName = {"上海", "杭州", "嘉兴", "绍兴", "武汉", "宁波"};
//        String[] cityName = {"武汉"};
        List<DongCheDiCarDto> list = new ArrayList<>();
        for (int i = 0; i < cityName.length; i++) {
            List<DongCheDiCarDto> dongCheDi = PaChongUtils.getDongCheDi(cityName[i]);
            list.addAll(dongCheDi);
        }

        // 导出
        ExcelUtil<DongCheDiCarDto> excelUtil = new ExcelUtil(DongCheDiCarDto.class);
        String export = excelUtil.exportExcel(list, "车辆记录");

        try {
            FileUtil.downloadFileOnce(export, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("文件输出失败");
        }
    }

    @PostMapping("/exportQiCheZhiJia")
    @ApiOperation(value = "导出汽车之家数据")
    public void exportQiCheZhiJia(HttpServletResponse response) {


        String[] cityName = {"shanghai", "hangzhou", "jiaxing", "shaoxing", "wuhan", "ningbo"};
//        String[] cityName = {"武汉"};
        List<DongCheDiCarDto> qiCheZhiJiaList = new ArrayList<>();
        for (int i = 0; i < cityName.length; i++) {
            List<DongCheDiCarDto> qiCheZhiJia = PaChongUtils.getQiCheZhiJia(cityName[i], "benchiglb");
            qiCheZhiJiaList.addAll(qiCheZhiJia);
        }
        // 导出
        ExcelUtil<DongCheDiCarDto> excelUtil = new ExcelUtil(DongCheDiCarDto.class);
        String export = excelUtil.exportExcel(qiCheZhiJiaList, "车辆记录");

        try {
            FileUtil.downloadFileOnce(export, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("文件输出失败");
        }
    }
}
